home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Russian
/
cpsimage.cab
/
data
/
sys
/
FileVerifier.elf
< prev
next >
Wrap
Text File
|
2009-04-23
|
5KB
|
159 lines
#load "sys/datetime.elf";
#load "sys/stdio.elf";
#load "sys/xipxml.elf";
/****************************************************************************/
/*
** This class provides the means to verify the integrity of a file by
** computing a MD5 hash value of the file and comparing that value to the
** corresponding file hash value maintained in a xml database file.
*/
/* @New
// DESCRIPTION
Creates a new instance of FileVerifier for file verification.
// ARGUMENTS
FILE dbfile xml database file.
// EXAMPLES
// Example 1: Create a FileVerifier object with empty database
FileVerifier fv = new (FileVerifier);
//-------------------------------------------------------
// Example 2: Create a FileVerifier object with existing database
FILE file = new (FILE, path: "testdb.xml");
FileVerifier fv = new (FileVerifier, dbfile: file);
*/
/* @isVerified Indicates if the integrity of a file is verified. */
/* @isDBModified Indicates if the xml database has been changed. */
/* @writeDBFile Writes the xml database to disk using specified directory. */
/****************************************************************************/
CLASS FileVerifier {
XmlDocument xmldb;
LIST indexdb;
BOOLEAN dbModified;
METHOD New (FILE dbfile) {
if (!dbfile) {
this.xmldb = XmlDocumentBuilder.newDocument ();
XmlElement root = this.xmldb.createElement (name: "FileVerification");
this.xmldb.setDocumentElement (node: root);
}
else {
if (!dbfile.exists ())
SetStatus (op: "Stop", msg: "FileNotFoundException\n");
if (!dbfile.isFile ())
SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
STRING path = dbfile.getCanonicalPath ();
this.xmldb = XmlDocumentBuilder.parseFile (filename: path);
XmlElement root = this.xmldb.getDocumentElement ();
XmlNode n;
INTEGER nentries = 0;
STRING name;
for (n = root.getFirstChild (); n; n = n.getNextSibling ()) {
if (n.getNodeType () != XmlNode.XML_ELEMENT_NODE)
continue;
name = n.getAttribute (name: "name");
this.indexdb.insert (entry: nentries, name: name, obj: n);
nentries++;
}
}
}
METHOD isVerified (FILE file)
RETURNS (BOOLEAN verified) {
if (!file)
SetStatus (op: "Stop", msg: "NullObjectException\n");
if (!file.exists ())
SetStatus (op: "Stop", msg: "FileNotFoundException\n");
if (!file.isFile ())
SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
STRING name = file.getName ();
STRING path = file.getCanonicalPath ();
STRING actHash = MD5 (filename: path);
INTEGER idx = this.findEntry (name: name);
if (idx < 0) {
// Input file does not exists (new)
this.addEntry (idx: -idx - 1, name: name, hash: actHash);
this.dbModified = TRUE;
}
else {
// Input file exists
XmlElement e = this.indexdb[idx];
STRING expHash = e.getAttribute (name: "hash");
if (actHash == expHash) {
// Input file is verified (hash values match)
verified = TRUE;
}
else {
// Input file is not verified (hash values differ)
e.setAttribute (name: "hash", value: actHash);
this.dbModified = TRUE;
}
}
}
METHOD isDBModified ()
RETURNS (BOOLEAN mod) {
mod = this.dbModified;
}
METHOD writeDBFile (FILE dir) {
DATETIME dt = DATETIME.now (utc: FALSE);
STRING dbpath = "db" + dt.toString () + ".xml";
if (dir) {
if (!dir.exists ())
dir.mkdirs ();
else if (!dir.isDir ())
SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
dbpath = dir.getCanonicalPath () + FILE.sep () + dbpath;
}
this.xmldb.dumpFile (filename: dbpath, format: 1);
}
private METHOD findEntry (STRING name)
RETURNS (INTEGER idx) {
INTEGER low = 0, high = this.indexdb.length () - 1;
STRING curName;
while (low <= high) {
idx = (low + high) >> 1;
curName = this.indexdb.name (entry: idx);
if (curName == name)
return;
if (curName < name)
low = idx + 1;
else
high = idx - 1;
}
idx = -(low + 1);
}
private METHOD addEntry (INTEGER idx, STRING name, STRING hash) {
XmlElement e = this.xmldb.createElement (name: "File");
e.setAttribute (name: "name", value: name);
e.setAttribute (name: "hash", value: hash);
XmlElement root = this.xmldb.getDocumentElement ();
INTEGER nentries = this.indexdb.length ();
if (idx < nentries)
root.insertBefore (newchild: e, refchild: this.indexdb[idx]);
else
root.appendChild (node: e);
this.indexdb.insert (entry: idx, name: name, obj: e);
}
}